<?php
/**
 * @name ApplicationController
 * @author duanChi <http://weibo.com/shijingye>
 * @desc 应用控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
class AppController extends Yaf\Controller_Abstract {
	/** 
     * 默认动作
     * 根据传入的模板参数显示页面并加入数据
     */
	public function indexAction($app_code = NULL, $data_type = NULL, $static = FALSE) {
		//初始化handler
		$_application = new \privateClass\App\Application();
		$_client = new \privateClass\App\Client();
		//格式化QUERY_STRING
		$_QUERY_STRING = $_application->parse_query_string($this->getRequest()->getMethod());
		
		//初始化参数
		$_AMS_CLIENT = $_client->get($_QUERY_STRING['query_settings']['AMSCID']);
		$_VARS = [];
		$_STREAM = '';
		$_CURRENT_RULE = [];
		$_RETURN = FALSE;
		$_TEMPLATE = ['path'=>'','file'=>''];
		$_CONF = $_application->get_config($app_code,$_QUERY_STRING, $static);
		$_time = time().date('ms').'-'.str_pad(mt_rand(0, 999),3,0);
		
		//默认路径
		if ($static == FALSE && empty($_QUERY_STRING['query_uri']) && $this->check_index_page($_CONF['app_id'])) $static = $_CONF['config']['index'];
		
		
		
		//判断时候静态页面
		if ($static == FALSE) {
			//获取当前有效的rule配置信息
			
			if ($_CONF != FALSE && !empty($_QUERY_STRING['query_uri'])) {
				//根据当前rule配置信息抓取页面内容
				$uri_response = $_application->fetch($_QUERY_STRING, $_CONF);
				//根据抓取内容解析缓存变量
				//根据要求的返回格式返回内容(html|xml|json|stream|msgpack)
				try {
					if ($_CONF['config']['data_type'] == 'stream') {
						$_STREAM = $_application->get_stream($uri_response['response'], $_CONF['config']);
					} else {
						$_VARS = $_application->get_vars($uri_response['response'], $_CONF['config'], $uri_response['charset']);
					}
				} catch (\Exception $e) {
					$_VARS = false;
				}
				
				//记录抓取页面缓存
				if ($uri_response != FALSE) {
					//记录日志
					//---------------log start
					$_frame = '[ams-service app-frame-log]
[request-frame]
url: '.$_QUERY_STRING['query_uri'].'
content:'.$uri_response['request'].'
				
[response-frame]
'.$uri_response['response_header'].'

'.$uri_response['response'].'
[response-vars]
'.var_export($_VARS,TRUE);
					//---------------log end
				}
				\privateClass\App\Log::applog(_PROJECT_TEMPLATE_ROOT.DIRECTORY_SEPARATOR.$_CONF['app_id'].DIRECTORY_SEPARATOR.PROJECT_CACHE_PATH.DIRECTORY_SEPARATOR.$_time.'.cache.amsf', $_frame);
				header('Dump-Cachefile: '.$_time.'.cache.amsf');
				
				
			}
			if ($data_type == NULL)$_TEMPLATE['file'] = $_CONF['config']['template'].$_CONF['template_extension'];
			
		} else {
			//如果是静态页面 直接取得静态页面
			$_TEMPLATE['file'] = $static;
		}
		//json(p)|msgpack接口模式判断,如果是stream模式则强制直接输出
		if ($_CONF['config']['data_type'] == 'stream') {
			header('Content-type: '.$_STREAM['content_type']);
			echo $_STREAM['content'];
		} elseif ($data_type == NULL) {
			$_TEMPLATE['path'] = _PROJECT_TEMPLATE_ROOT.DIRECTORY_SEPARATOR.$_CONF['app_id'].DIRECTORY_SEPARATOR.PROJECT_WWW_PATH.DIRECTORY_SEPARATOR;
			$this->getView()->assign('_TEMPLATE',$_TEMPLATE);
			$this->getView()->assign('_TEMPLATE_FILE_PATH',$_TEMPLATE['path'].$_TEMPLATE['file']);
			$this->getView()->assign('_VARS',$_VARS);
			$_RETURN = TRUE;
		} else {
			_echo($_VARS, $data_type, isset($_QUERY_STRING['query_settings']['callback']) ? $_QUERY_STRING['query_settings']['callback'] : '');
		}
		return $_RETURN;
	}
	
	public function staticAction($app_code = NULL, $static = FALSE) {
		return $this->indexAction($app_code, NULL, $static);
	}
	
	private function check_index_page($app_id) {
		return file_exists(_PROJECT_TEMPLATE_ROOT.DIRECTORY_SEPARATOR.$app_id.DIRECTORY_SEPARATOR.PROJECT_WWW_PATH.DIRECTORY_SEPARATOR.'index.html');
	}
}